package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	/**
	 * 获取登录用户信息
	 * @return
	 */
	@Operation(summary = "获取登录用户信息")
	@GetMapping("/wxLogin/getUserInfo")
	@GuiGuLogin
	public Result<UserInfoVo> getUserInfo(){
		// 1.获取当前用户的id
		Long userId = AuthContextHolder.getUserId();
		// 2.调用业务逻辑层获取用户信息
		UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
		return Result.ok(userInfoVo);
	}

	/**
	 * 更新用户信息
	 */
	@Operation(summary = "更新用户信息")
	@PostMapping("/wxLogin/updateUser")
	@GuiGuLogin
	public Result updateUser(@RequestBody UserInfoVo userInfoVo){
		userInfoService.updateUser(userInfoVo);
		return Result.ok();
	}

	/**
	 * 根据用户ID查询用户信息
	 * @param userId
	 * @return
	 */
	@Operation(summary = "根据用户ID查询用户信息")
	@GetMapping("/userInfo/getUserInfoVo/{userId}")
	@GuiGuCache(prefix = "cache:userInfo:")
	public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId){
		UserInfoVo userInfo = userInfoService.getUserInfo(userId);
		return Result.ok(userInfo);
	}

	/**
	 *	提供给专辑服务动态判断声音付费标识业务接口，其中请求体参数声音ID集合，每一页中声音ID都需要提交来进行验证购买情况
	 * @param userId  用户ID
	 * @param albumId 专辑ID
	 * @param needCheckTrackList 待检查购买情况声音ID集合
	 * @return {专辑ID：购买结果} 1：已购买 0：未购买
	 */
	@Operation(summary = "根据用户ID+专辑ID+声音ID列表动态判断声音购买情况")
	@PostMapping("/userInfo/userIsPaidTrack/{userId}/{albumId}")
	public Result<Map<Long,Integer>> userIsPaidTrack(@PathVariable Long userId, @PathVariable Long albumId,
													 @RequestBody List<Long> needCheckTrackList){
		Map<Long,Integer> map = userInfoService.userIsPaidTrack(userId,albumId,needCheckTrackList);
		return Result.ok(map);
	}

	/**
	 * 根据专辑ID判断用户是否买过该专辑
	 * @param albumId
	 * @return
	 */
	@Operation(summary = "根据专辑ID判断用户是否买过该专辑")
	@GetMapping("/userInfo/isPaidAlbum/{albumId}")
	@GuiGuLogin
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId){
		Boolean isPaid = userInfoService.isPaidAlbum(albumId);
		return Result.ok(isPaid);
	}

	/**
	 * 根据专辑id+用户ID获取用户已购买声音id列表
	 * @param albumId
	 * @return
	 */
	@Operation(summary = "根据专辑id+用户ID获取用户已购买声音id列表")
	@GetMapping("/userInfo/findUserPaidTrackList/{albumId}")
	@GuiGuLogin
	public Result<List<Long>> getUserPaidTrackList(@PathVariable Long albumId){
 		List<Long> userPaidTrackIdList =  userInfoService.getUserPaidTrackList(albumId);
		 return Result.ok(userPaidTrackIdList);
	}


	/**
	 * 提交订单支付（余额，支付宝）成功-虚拟物品发货
	 * @param userPaidRecordVo
	 * @return
	 */
	@Operation(summary = "提交订单支付（余额，支付宝）成功-虚拟物品发货")
	@PostMapping("/userInfo/savePaidRecord")
	public Result savePaidRecord(@RequestBody UserPaidRecordVo userPaidRecordVo){
		userInfoService.savePaidRecord(userPaidRecordVo);
		return Result.ok();
	}

}

